package com.zby.controller.login;

import com.zby.common.utils.ResultUtil;
import com.zby.model.pojo.User;
import com.zby.model.vo.LoginUser;
import com.zby.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wuyanzhu
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户登录")
@Slf4j
public class UserLoginController {
    @Autowired
    public UserService userService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public ResultUtil<String> login(@RequestBody LoginUser loginUser) {
        String token = userService.login(loginUser);
        return ResultUtil.success(token);
    }

    // 注册
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public ResultUtil<User> register(@RequestBody User user) {
        // 添加日志
        log.info("Received user: {}", user);
        userService.register(user);
        return ResultUtil.success();
    }
    // Refresh Token
    @PostMapping("/refresh")
    @Operation(summary = "刷新令牌")
    public ResultUtil<String> refreshToken(@RequestBody String refreshToken) {
        log.info("Received refresh token: {}", refreshToken);
        return ResultUtil.success(userService.refreshToken(refreshToken));
    }
}
